package com.chen.gui.icon;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.geom.Area;
import java.awt.geom.GeneralPath;
import java.awt.geom.RoundRectangle2D;

import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JLabel;

import com.chen.Shell;
import com.chen.qq.Context;

public class IconContact implements Icon {

	public static final int contact = 0;

	public static final int group = 1;

	public static final int latest = 2;

	private boolean selected = true;

	private int mode = 1;

	@Override
	public void paintIcon(Component c, Graphics g, int x, int y) {
		Graphics2D g2d = (Graphics2D) g.create();
		g2d.translate(x, y);

		if (mode == group) {
			g2d.setTransform(AffineTransform.getScaleInstance(.8, .8));
			paint(g2d);
			g2d.translate(5, 3);
			paint(g2d);
		} else {
			paint(g2d);
			if (mode == latest) {
				g2d.setColor(selected ? Color.white : Context.iconColor);
				int iconWidth = getIconWidth();
				int iconHeight = getIconHeight();
				int x1 = iconWidth / 2 - 1;
				int y1 = 4;
				int y2 = iconHeight / 2;
				g2d.drawLine(x1, y1, x1, y2);
				g2d.drawLine(x1, y2, x1 + 5, y2);
			}
		}

		g2d.dispose();

	}

	private void paint(Graphics2D g2d) {
		RoundRectangle2D rect = new RoundRectangle2D.Float(1, 1, getIconWidth() - 2, getIconHeight() - 5, 5, 5);
		GeneralPath gp = new GeneralPath();

		double startY = rect.getY() + rect.getHeight();
		float offset = 5;
		gp.moveTo(offset, startY);
		gp.lineTo(offset - 1, startY + 3);
		gp.lineTo(offset + 4, startY);

		Area area = new Area(rect);
		area.add(new Area(gp));

		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

		if (selected) {
			g2d.setColor(Context.iconColor);
			g2d.fill(area);
		}

		g2d.setColor(Color.white);
		g2d.draw(area);
	}

	@Override
	public int getIconWidth() {
		return 16;
	}

	@Override
	public int getIconHeight() {
		return 16;
	}

	public static void main(String[] args) {
		JComponent btn = new JLabel(new IconContact());
		btn.setOpaque(true);
		btn.setBackground(Color.black);
		Shell.run(btn);
	}
}
